﻿'Interface créer par Caroline St-Yves
'Programmez par Caroline St-Yves
'Date de création : 5 septembre 2012, 8h00 am.
'Date dernière modification : 11 octobre 2012, 23h50. 'effectué par Caroline
Imports System.Data.SqlClient
Public Class GestionFormation
    Dim selectID As Integer
    Dim oLocalContext As DBMainContext
    Public oListFormation As New List(Of Formation)
    Sub loadDG()
        oLocalContext = New DBMainContext
        Try
            Dim evensQuery = From u In oLocalContext.Formation
                             Where Not oListFormation.Contains(u)
                             Select u

            Dim array() = evensQuery.ToArray()

            dgvFormation.DataSource = evensQuery
            dgvFormation.Columns(0).Visible = False
            dgvFormation.Columns(2).Visible = False

        Catch ex As Exception
            Console.Write(ex.Message)
            Console.Write("Toutes les formations ont été trouvées!")
        End Try

    End Sub
    Private Sub GestionFormation_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oLocalContext = New DBMainContext
        Try
            Dim evensQuery = From u In oLocalContext.Formation
                             Where Not oListFormation.Contains(u)
                             Select u

            Dim array() = evensQuery.ToArray()

            dgvFormation.DataSource = evensQuery
            dgvFormation.Columns(0).Visible = False
            dgvFormation.Columns(2).Visible = False

        Catch ex As Exception
            Console.Write(ex.Message)
            Console.Write("Toutes les formations ont été trouvées!")
        End Try
        
    End Sub

    Private Sub btnSauvegarder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSauvegarder.Click
        oLocalContext.CustomSubmitChanges()
    End Sub

    Private Sub dgvFormation_RowValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvFormation.RowValidated
        Try
            oLocalContext.CustomSubmitChanges()

        Catch sqlExp As SqlException

            If (sqlExp.Number = 547) Then

                MsgBox("Cette formation ne peut être supprimé, car il est lié à des enregistrements", MsgBoxStyle.Critical)
                CType(Me.Parent, CustomMessageBox).Close()

            End If

        End Try
    End Sub

    Public Function GetSelectedItems() As List(Of Formation)
        Return (From u As DataGridViewRow In dgvFormation.SelectedRows Select CType(u.DataBoundItem, Formation)).ToList

    End Function

    Private Sub btnAjoutFormation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjoutFormation.Click
        dgvFormation.Rows(dgvFormation.Rows.Count - 1).Selected = True
    End Sub

    Private Sub btnSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupprimer.Click

        'Dim answer As DialogResult
        'answer = MessageBox.Show("Voulez-vous supprimer cette formation?", "Yes ou Non", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        'If answer = vbYes Then

        '    Dim deleteForm = (From cust In oLocalContext.Formation
        '               Where cust.ID = selectID).ToList()

        '    oLocalContext.Formation.DeleteOnSubmit(deleteForm(0))
        '    loadDG()

        '    Try
        '        oLocalContext.SubmitChanges()
        '    Catch
        '        ' Handle exception.
        '    End Try
        'End If
    End Sub

    Private Sub dgvFormation_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvFormation.CellClick
        selectID = dgvFormation.Rows(dgvFormation.SelectedCells(0).RowIndex).Cells(0).Value
    End Sub

End Class
